﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SysMisc;
using Kernel;

namespace SppdProjectHelper
{
    public partial class pad_mng : UserControlEx//,IControlEx
    {
        public pad_mng()
        {
            InitializeComponent();
        }

        /*控件只有此种实例化方式*/
        public pad_mng(IControlPad pad)
            : this()
        {
            /*传入参数*/
            _pad = pad;
            this.Dock = DockStyle.Fill;
        }

        public Project Project { get; set; }

        /*私有变量*/
        private IControlPad _pad = null;

        protected override void UI_Init()
        {
            this.AddExControl(this.toolStrip_main);

            this.InitAllExControls();
        }

        protected override void UI_Load()
        {
            funcUpdateDataToUI();

            this.LoadAllExControls();
        }

        protected override void UI_Update()
        {
            this.UpdateAllExControls();
        }

        protected override void UI_Save()
        {
            throw new NotImplementedException();
        }

        private void funcUpdateDataToUI()
        {
            if(Project != null)
            {
                ProjectConfig prj_cfg = Project.ProjectConfig;
                {//主配置
                    this.textBox_cmpyCode.Text = prj_cfg.MainCfg.CmpyCode;
                    this.textBox_prjCode.Text = prj_cfg.MainCfg.PrjCode;
                    this.textBox_prjName.Text = prj_cfg.MainCfg.PrjName;
                    this.textBox_subject.Text = prj_cfg.MainCfg.Subject;
                    this.textBox_state.Text = prj_cfg.MainCfg.State;
                    this.textBox_volName.Text = prj_cfg.MainCfg.VolName;
                    this.textBox_volNum.Text = prj_cfg.MainCfg.VolNum.ToString();
                    this.textBox_rowNum.Text = prj_cfg.MainCfg.RowNum.ToString();
                    this.textBox_pubDate.Text = prj_cfg.MainCfg.PubDate;
                    this.textBox_startNum.Text = prj_cfg.MainCfg.StartNum.ToString();
                }
            }
        }

        private void funcUpdateDataFromUI()
        {
            ProjectConfig prj_cfg = Project.ProjectConfig;
            {//主配置
                try
                {
                    prj_cfg.MainCfg.CmpyCode = this.textBox_cmpyCode.Text;
                    prj_cfg.MainCfg.PrjCode = this.textBox_prjCode.Text;
                    prj_cfg.MainCfg.PrjName = this.textBox_prjName.Text;
                    prj_cfg.MainCfg.Subject = this.textBox_subject.Text;
                    prj_cfg.MainCfg.State = this.textBox_state.Text;
                    prj_cfg.MainCfg.VolName = this.textBox_volName.Text;
                    prj_cfg.MainCfg.VolNum = Convert.ToInt32(this.textBox_volNum.Text);
                    prj_cfg.MainCfg.RowNum = Convert.ToInt32(this.textBox_rowNum.Text);
                    prj_cfg.MainCfg.PubDate = this.textBox_pubDate.Text;
                    prj_cfg.MainCfg.StartNum = Convert.ToInt32(this.textBox_startNum.Text);
                }catch(Exception ex)
                {
                    SysMisc.LogHelper.Error(typeof(pad_mng), ex);
                }
            }
        }


        private void ToolStripButton_Click(object sender, EventArgs e)
        {
            ToolStripButton tsb = (ToolStripButton)sender;
            switch (tsb.Text)
            {
                case "工程文件":
                    {
                        pad_file pp = new pad_file(_pad);
                        pp.Project = this.Project;
                        _pad.ShowNewPad(pp);
                    }
                    break;
                case "图纸目录":
                    {
                        pad_catg pp = new pad_catg(_pad);
                        pp.Project = this.Project;
                        _pad.ShowNewPad(pp);
                    }
                    break;
                case "打印":
                    {
                        pad_print pp = new pad_print(_pad);
                        pp.Project = this.Project;
                        _pad.ShowNewPad(pp);
                    }
                    break;
                case "生成校审单":
                    {
                        //using (Form_FastReportPrinter frmCat = new Form_FastReportPrinter())
                        //{
                        //    frmCat.ShowInTaskbar = false;
                        //    frmCat.ProjectConfig = this.Project.ProjectConfig;
                        //    DialogResult dr = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frmCat);
                        //}
                    }
                    break;
            }
        }

        //“确认修改按钮”，用来保存工程配置
        private void button_saveMainAtt_Click(object sender, EventArgs e)
        {
            funcUpdateDataFromUI();
            Misc.Alert("已成功修改。如需保存到配置文件，还要点击【保存工程】。");
            Project.IsUpdated = true;
        }

        #region
        //void IControlEx.Init() { UI_Init(); }
        //void IControlEx.Load() { UI_Load(); }
        //void IControlEx.Update() { UI_Update(); }
        //void IControlEx.Save() { }
        //bool IControlEx.Updated
        //{
        //    get { return false; }
        //}
        #endregion
    }
}
